package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.dto.OrderSettingDTO;
import com.itheima.service.OrderSettingService;
import com.itheima.vo.OrderSettingVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.util.List;


/**
 * @author 32338
 * @date 2024-05-04 15:17:05
 * @description 预约设置管理
 */
@RestController
@RequestMapping("/ordersetting")
@Slf4j
public class OrderSettingController {

    @Autowired
    private OrderSettingService orderSettingService;

    /**
     * @description
     ***:根据日期修改可预约人数
     **/
    @PostMapping("/editNumberByOrderDate")
    public Result editNumberByOrderDate(@RequestBody OrderSettingDTO orderSettingDTO){
        log.info("根据日期修改可预约人数:{}",orderSettingDTO);

        orderSettingService.editNumberByOrderDate(orderSettingDTO);

        return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
    }

    /**
     * @description
     ***:根据年月查询对应的预约设置信息
     **/
    @GetMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String month ){
            log.info("根据年月查询对应的预约设置信息:{}",month);

        List<OrderSettingVO> orderSettingVOList = orderSettingService.getOrderSettingByMonth(month);

        return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,orderSettingVOList);
    }

    /**
     * @description
     ***:模板下载
     **/
    @GetMapping("/download")
    public void  download(@RequestParam String filename, HttpServletResponse Response){
        log.info("模板下载");
        orderSettingService.download(filename,Response);
    }

    /**
     * @description
     ***:文件上传模板
     **/
    @PostMapping("/upload")
    public Result export(MultipartFile excelFile){
        log.info("文件上传模板");
        //判断是否为xlxs文件
        String fileName=excelFile.getOriginalFilename();
        String last=fileName.substring(fileName.lastIndexOf("."));
        if(!last.equals(".xlsx")){
            return new Result(false,"文件格式不匹配");
        }
        orderSettingService.upload(excelFile);
        return new Result(true,"设置预约成功");
    }

}
